from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
#chrome选项
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
# 防止打印一些无用的日志
options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
prefs = {"credentials_enable_service": False,
         "profile.password_manager_enabled": False}
options.add_experimental_option("prefs", prefs)

#配置并获得WebDriver对象
driver = webdriver.Chrome(options=options)
# driver = webdriver.Chrome()

def login():
    driver.get('http://120.77.147.7/')
    driver.maximize_window()
    user_box = driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/form/div[1]/div/div[1]/input')
    pwd_box = driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/form/div[2]/div/div/input')
    login_btn = driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/form/button')
    user_box.send_keys('3290274862@qq.com')
    pwd_box.send_keys('123456abc')
    login_btn.click()


def add_func(funcname):
    driver.get('http://120.77.147.7/#/functions-library')
    time.sleep(3)
    new_btn = driver.find_element(By.XPATH, '//*[@id="app"]/div/main/div/section/main/div[1]/div/button[1]')
    new_btn.click()
    time.sleep(2)
    func_name_box = driver.find_element(By.XPATH, '//*[@id="app"]/div/main/div/div/div/div/form/div[1]/div/div/input')
    func_name_box.send_keys(funcname)
    submit_btn = driver.find_element(By.XPATH, '//*[@id="app"]/div/main/div/div/div/div/div[1]/button[1]')
    submit_btn.click()
    time.sleep(2)
    func_row = driver.find_element(By.XPATH, f'//tr[td/div[contains(text(), \'{funcname}\')]]')
    edit_btn = func_row.find_element(By.XPATH, '//*[@id="function-edit-guide"]/button[2]')
    edit_btn.click()
    time.sleep(2)
    dep_manage_btn = driver.find_element(By.XPATH, '//*[@id="tab-third"]')
    dep_manage_btn.click()
    time.sleep(2)
    search_box = driver.find_element(By.XPATH, '//*[@id="pane-third"]/div/div[2]/div/div[4]/input')
    search_box.send_keys('sympy')
    time.sleep(2)
    add_btn = driver.find_element(By.XPATH, '//*[@id="pane-third"]/div/div[2]/div/div[5]/div/div[1]/div[3]/table/tbody/tr[1]/td[5]/div/div/button')
    add_btn.click()
    search_box.clear()
    search_box.send_keys('numpy')
    time.sleep(2)
    add_btn.click()
    update_dep_btn = driver.find_element(By.XPATH, '//*[@id="pane-third"]/div/div[1]/button[1]')
    update_dep_btn.click()
    time.sleep(1)
    ensure_btn = driver.find_element(By.XPATH, '/html/body/div[4]/div/div[3]/button[2]')
    ensure_btn.click()
    time.sleep(2)
    update_btn = driver.find_element(By.XPATH, '//*[@id="pane-third"]/div/div[1]/button[1]')
    update_btn.click()
    time.sleep(30)
    driver.find_element(By.XPATH, '//*[@id="tab-first"]').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="pane-first"]/div/div[1]/button').click()
    time.sleep(2)


if __name__ == '__main__':
    login()
    # add_func('logical')
    # func_list = ['and','not','or','xor','all','any','hasSymType','isAlways','isfinite','isinf','isnan','logic']
    func_list = ['And', 'Not', 'Or']
    for func in func_list:
        add_func(func)
    # run_task2()
    # run_task3()
    # run_task4()
    # run_task5()
